import json
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
#定义工具类
class UtilsDriver:
    #表示自媒体运营系统的浏览器驱动
    _mp_driver = None
    # 表示后台管理系统的浏览器驱动
    _mis_driver = None
    # 后台管理系统退出驱动的标识
    _quit_mis_driver = True
    #定义修改私有属性的方法
    @classmethod
    def set_quit_driver(cls, mark):
        cls._quit_mis_driver = mark
    #定义获取自媒体运营系统的浏览器驱动
    @classmethod
    def get_mp_driver(cls):
        if cls._mp_driver is None:
           cls._mp_driver = webdriver.Chrome()
           cls._mp_driver.maximize_window()
           cls._mp_driver.get("http://pc-toutiao-python.itheima.net/#/login")
        return cls._mp_driver
    #定义退出自媒体运营系统的浏览器驱动
    @classmethod
    def quit_mp_driver(cls):
        if cls._mp_driver is not None:
           cls.get_mp_driver().quit()
           cls._mp_driver = None
    #定义获取后台管理系统的浏览器驱动
    @classmethod
    def get_mis_driver(cls):
        if cls._mis_driver is None:
           cls._mis_driver = webdriver.Chrome()
           cls._mis_driver.maximize_window()
           cls._mis_driver.get("http://mis-toutiao-python.itheima.net/#/")
        return cls._mis_driver
    # 定义退出后台管理系统的浏览器驱动
    @classmethod
    def quit_mis_driver(cls):
        if cls._mis_driver and cls._quit_mis_driver:
           cls._mis_driver.quit()
           cls._mis_driver = None
#封装自媒体运营系统选择频道的方法
def choice_channel(driner, element, channel):
    element.click()
    time.sleep(1)
    xpath = "//*[@class='el-select-dropdown__wrap el-scrollbar__wrap']""//*[text()='{}']".format(channel)
    driner.find_element(By.XPATH, xpath).click()

#封装一个方法，用于判断元素是否存在
def is_exist(driver, text):
    xpath = "//*[contains(text(), '{}')]".format(text)
    try:
        time.sleep(2)
        return driver.find_element(By.XPATH,xpath)
    except Exception as e:
        return False
#获取测试数据的方法
def get_case_data(filename):
    with open(filename, encoding='utf-8') as f:
        case = json.load(f)
    list_case_data = []
    for case_data in case.values():
        list_case_data.append(tuple(case_data.values()))
    return  list_case_data

